var pomelo = window.pomelo;
var host = "127.0.0.1";
var port = "4150";

function connect() {
    pomelo.removeAllListeners();
    pomelo.disconnect();
    queryEntry(function(host, port, sid) {
        pomelo.init({
            host: host,
            port: port,
            log: true
        }, function() {
            showMsg('连接服务器成功...');
            listenEvent();
        });
    });
}

function register() {
    var username = document.getElementById("name").value;
    var passwd = document.getElementById("passwd").value;
    if (!username || !passwd) {
        alert('帐号/密码不能为空!!!');
    } else {
        var route = "connector.entryHandler.register";
        pomelo.request(route, {name: username, passwd: passwd}, function(data) {
            showMsg('【系统消息】' + data.msg);
        });
    }
}

function login() {
    var username = document.getElementById("name").value;
    var passwd = document.getElementById("passwd").value;
    if (!username || !passwd) {
        alert('帐号/密码不能为空');
    } else {
        var route = "connector.entryHandler.login";
        pomelo.request(route, {name: username, passwd: passwd}, function (data) {
            showMsg('【系统消息】' + data.msg);
        });
    }
}

function change_scene() {
    var route = "scene.sceneHandler.changeScene";
    pomelo.request(route, {}, function (data) {
        showMsg('【系统消息】' + data.msg);
    });
}

function global_broadcast() {
    var msg = document.getElementById("msg").value;
    var route = "connector.entryHandler.global_broadcast";
    pomelo.notify(route, {msg: msg});
}

function scene_chat() {
    var msg = document.getElementById("msg").value;
    var route = "scene.sceneHandler.chat";
    pomelo.notify(route, {msg: msg});
}

function private_chat() {
    var uid = document.getElementById("uid").value;
    var msg = document.getElementById('private_msg').value;
    var route = "connector.entryHandler.private_chat";
    pomelo.request(route, {uid: uid, msg: msg}, function(data) {
        showMsg('【系统消息】' + data.msg);
    });
}

function logout() {
    var route = "connector.entryHandler.logout";
    pomelo.request(route, {}, function(data) {
        showMsg('【系统消息】' + data.msg);
        pomelo.removeAllListeners();
        pomelo.disconnect();
    });
}
function listenEvent() {
    pomelo.on('onJoin', function(data) {
        showMsg('【系统消息】玩家进入场景: username: ' + data.name + ', sid: ' + data.sid);
    });

    pomelo.on('onUserInfo', function(data) {
        showMsg('【系统消息】场景信息：');
        var members = data.members;
        for(var index in members) {
            showMsg('    user: ' + members[index].name + ', sid: ' + members[index].sid);
        }
    });

    pomelo.on('onLeave', function(data) {
        showMsg('【系统消息】玩家离开场景: username: ' + data.uid + ', sid: ' + data.sid);
    });

    pomelo.on('onLogout', function(data) {
        showMsg('【系统消息】玩家下线: username: ' + data.uid + ', sid: ' + data.sid);
    });

    pomelo.on('onJoinScene', function(data) {
        showMsg('【系统消息】' + data.msg);
    });

    pomelo.on('onChat', function(data) {
        switch (data.type) {
            case 1:
                showMsg('【世界频道】' + data.msg);
                break;
            case 2:
                showMsg('【场景频道】' + data.msg);
                break;
            case 3:
                showMsg('【私聊频道】' + data.msg);
                break;
            default:
                break;
        }
    });
}

function queryEntry(cb) {
    var route = "gate.gateHandler.queryEntry";
    pomelo.init({
        host: host,
        port: port,
        log: true
    }, function(){
        pomelo.request(route, {}, function(data) {
            pomelo.disconnect();
            if(data.code === 500) {
                showMsg('gate error.');
                return;
            }
            showMsg('服务器IP: ' + data.host + ' 服务器端口：' + data.port + ' 服务器名称：' + data.sid);
            cb(data.host, data.port, data.sid);
        });
    });
}


function showMsg(msg) {
    var area = document.getElementById("showMsg");
    area.value = area.value + "\n" + msg;
    area.scrollTop = area.scrollHeight;
}